#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int trap(vector<int>& height)
    {
        int left = 0, right = height.size() - 1;
        int ret = 0;
        int c_left_val = height[left], c_right_val = height[right];
        while (left < right)
        {
            if (c_left_val < c_right_val)
            {
                ret += c_left_val - height[left];
                left++;
                if (height[left] > c_left_val)
                {
                    c_left_val = height[left];
                }
            }
            else
            {
                ret += c_right_val - height[right];
                right--;
                if (height[right] > c_right_val)
                {
                    c_right_val = height[right];
                }
            }
        }
        return ret;
    }
};